package com.fire.eyes.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fire.core.base.controller.BaseController;
import com.fire.core.mode.TMitUserToken;
import com.fire.core.service.CkeckTokenService;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.BlogUtil;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.model.*;
import com.fire.eyes.service.*;
import com.fire.eyes.validation.ProprietorNoToken;
import com.fire.eyes.validation.SiteIdDTO;
import com.fire.eyes.validation.TokenDTO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 *
 * @ClassName: SiteController
 * @Description: 站点控制器
 * @author zhongzifeng
 * @date 2019/1/16 20:27
 *
 */
@Controller
public class ProprietorController extends BaseController {
	private final static Logger blogin = Logger.getLogger(ProprietorController.class);

	@Autowired
	private ISiteService iSiteService;
	@Autowired
	private CkeckTokenService ckeckTokenService;
	@Autowired
	private IUserAuthorityService iUserAuthorityService;
	@Autowired
	private IFireSupervisorUserService iFireSupervisorUserService;
	@Autowired
	private IFireMaintainUserService iFireMaintainUserService;
	@Autowired
	private IFireProprietorService iFireProprietorService;
	@Autowired
	private IFireProprietorUserService iFireProprietorUserService;


	/**
	 * 查询消防站点列表
	 * 用于App向fire-app-server获得站点列表请求
	 * @author zhongzifeng
	 * @param tokenDTO 传入参数
	 * @param bindingResult 参数校验结果
	 * @return
	 */
	@PostMapping(value = "/myproprietors")
	@ResponseBody
	public AjaxResult myProprietors(@RequestBody @Validated TokenDTO tokenDTO, BindingResult bindingResult) {
		blogin.info("ProprietorController.myProprietors获取到的消息参数" + tokenDTO.toString());
		BlogUtil.inlog(JSON.toJSONString(tokenDTO),"","/fire/eyes/myproprietors","myProprietors","req");
		AjaxResult result;
		if (bindingResult.hasErrors()){
			for (FieldError fieldError : bindingResult.getFieldErrors()) {
				blogin.error("ProprietorController.myProprietors().error  错误为："+fieldError.getDefaultMessage());
				result = ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, fieldError.getDefaultMessage(), fieldError.getDefaultMessage());
				return result;
			}
			System.out.print("效验bu通过：）");
		}
		try {
			TMitUserToken token = ckeckTokenService.ckeckToken(tokenDTO.getToken());
			if(token == null){
				blogin.error("ProprietorController.myProprietors().error  错误为：无token记录");
				result = ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "无token记录", "无token记录");
				return result;
			}
			Integer userId = token.getUserid();
			Wrapper<UserAuthorityModel> userAutorityModel = new EntityWrapper<>();
			userAutorityModel.eq("userid",userId);
			UserAuthorityModel userAuthority = iUserAuthorityService.selectOne(userAutorityModel);
			String suthority = userAuthority.getAuthority();
			String column = "";
			String columnValue = "";

			// 监管单位
			String supervisorStr = "supervisor";
			if(!supervisorStr.equals(suthority)){
				result = ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功","", "操作成功");
				BlogUtil.inlog(JSON.toJSONString(result),"","/fire/eyes/myproprietors","myProprietors","resp");
				return result;
			}
			Wrapper<FireSupervisorUserModel> supervisorUserWrapper = new EntityWrapper<>();
			supervisorUserWrapper.eq("userid",userId);
			FireSupervisorUserModel supervisorUser = iFireSupervisorUserService.selectOne(supervisorUserWrapper);
			String supervisor = supervisorUser.getSupervisor();
			Integer page = tokenDTO.getPage();
			Integer pageSize = tokenDTO.getPagesize();
			Page<Map<String, Object>> pageObj = null;
			pageObj = new Page<>(page,pageSize);
			Page<Map<String,Object>> proprietorPage = iFireProprietorService.queryMyProprietorsList(pageObj,supervisor);
			List proprietorPageList = proprietorPage.getRecords();
			result = ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功",proprietorPageList, "操作成功");
			BlogUtil.inlog(JSON.toJSONString(result),"","/fire/eyes/myproprietors","myProprietors","resp");
		} catch (Exception e){
			e.printStackTrace();
			blogin.error("ProprietorController.myProprietors().error  错误为：" + e);
			result = ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "服务器开小差", "服务器开小差");
		}
		return result;
	}

	/**
	 * 我管理的业主单位详情
	 * 此接口用于App向fire-app-server消除告警接口请求
	 * @author zhongzifeng
	 * @param proprietorNoToken 传入参数
	 * @param bindingResult 参数校验结果
	 * @return
	 */
	@PostMapping(value = "/proprietordetails")
	@ResponseBody
	public AjaxResult proprietorDetails(@RequestBody @Validated ProprietorNoToken proprietorNoToken, BindingResult bindingResult) {
		blogin.info("ProprietorController.proprietorDetails获取到的消息参数" + proprietorNoToken.toString());
		BlogUtil.inlog(JSON.toJSONString(proprietorNoToken),"","/fire/eyes/proprietordetails","proprietorDetails","req");
		AjaxResult result;
		if (bindingResult.hasErrors()){
			for (FieldError fieldError : bindingResult.getFieldErrors()) {
				blogin.error("ProprietorController.proprietorDetails().error  错误为："+fieldError.getDefaultMessage());
				result = ajaxResult(ErrorCodeConstants.PARAM_WRONGFULL, fieldError.getDefaultMessage(), fieldError.getDefaultMessage());
				return result;
			}
			System.out.print("效验bu通过：）");
		}
		String proprietor = proprietorNoToken.getProprietor();
		Map<String,Object> map = iFireProprietorService.queryProprietor(proprietor);
		result = ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功",map, "操作成功");
		BlogUtil.inlog(JSON.toJSONString(result),"","/fire/eyes/proprietordetails","proprietorDetails","resp");
		return result;
	}
}
